<?php
include "header.php";
include "mysqli_connection.php";

if (!isset($_SESSION["username"])){
	echo "<script>window.location.href='login.php'</script>";
}
else{
?>
<script>change_picture("admin");</script>
<div>
<h1>Manage Programs</h1>
<a href = "managehome.php">Back to Manage Home</a>
</div>
<div>
<form action = "#" method = "GET">
<label for = "major">Major: </label><input type = "text" name = "major" value = "<?php echo $_GET['major'];?>" />
<label for = "entry_year">Entry Year: </label><input type = "text" name = "entry_year" value = "<?php echo $_GET['entry_year'];?>" />
<label for = "starting_course">Starting Course: </label>
<select name = "starting_course" >
<option value="0" <?php if ($_GET["starting_course"] === "") echo "Selected";?>>(None)</option>
<option value="1" <?php if ($_GET["starting_course"] == 1) echo "Selected";?>>CS 105</option>
<option value="2" <?php if ($_GET["starting_course"] == 2) echo "Selected";?>>CS 115</option>
<option value="5" <?php if ($_GET["starting_course"] == 5) echo "Selected";?>>CS 181</option>
</select>
<input type = "submit" value = "Search" />
<input type = "button" value = "Clear" onclick = "window.location.href = 'manageprograms.php';" />
</form>
</div>
<div>
<a href="editprogram.php">Add a new program</a>
</div>
<hr />
<div>
<table>
<thead>
<td>ID</td><td>Major</td><td>Entry Year</td><td>Starting Course</td><td>Requirements</td><td>Operation</td>
</thead>
<tbody>
<?php
$filter = "true";
if (isset($_GET["index"]) && $_GET["index"] !== ""){
	$param = "(p_id = " . $mysqli->real_escape_string(trim($_GET["index"])) . ")";
	$filter .= " AND " . $param;
}
if (isset($_GET["major"]) && $_GET["major"] !== ""){
	$param = "(major = '" . $mysqli->real_escape_string(trim($_GET["major"])) . "')";
	$filter .= " AND " . $param;
}
if (isset($_GET["entry_year"]) && $_GET["entry_year"] !== ""){
	$param = "(entry_year = '" . $mysqli->real_escape_string(trim($_GET["entry_year"])) . "')";
	$filter .= " AND " . $param;
}
if (isset($_GET["starting_course"]) && $_GET["starting_course"] != 0){
	$param = "(starting_course = " . $mysqli->real_escape_string(trim($_GET["starting_course"])) . ")";
	$filter .= " AND " . $param;
}
$perNumber = 25;
$page = $_GET['page'];
$count = $mysqli->query("SELECT count(*) FROM Programs " . $innerFilter . " WHERE " . $filter . ";");
$rs = mysqli_fetch_array($count);
$totalNumber = $rs[0];
if ($totalNumber == 0){
	echo "<div><font color='red'>No result!</font></div>";
}
else{
	echo "<div>Found " . $totalNumber . " programs.</div>";
}
$totalPage = ceil($totalNumber / $perNumber);
if ($page < 1) {
	$page = 1;
}
else if ($page > $totalPage){
	$page = $totalPage;
}
$startCount = ($page - 1) * $perNumber;
$sql = "SELECT Programs.p_id, Programs.major, Programs.entry_year, Courses.c_id, Courses.prefix, Courses.number FROM Programs LEFT JOIN Courses ON (Programs.starting_course = Courses.c_id) WHERE (" . $filter .") LIMIT $startCount, $perNumber";
$result = $mysqli->query($sql);
if ($result){
	while ($row = mysqli_fetch_array($result)){
?>
<tr>
<td><?php echo $row["p_id"];?></td>
<td><?php echo $row["major"];?></td>
<td><?php echo $row["entry_year"];?></td>
<td><?php echo $row["prefix"];?>&nbsp<?php echo $row["number"];?></td>
<td><a href="javascript:" id = "toggle_requirement_link_<?php echo $row["p_id"];?>" onclick = "toggle_requirement_list(<?php echo $row["p_id"];?>);false;">Show</a><div id = "toggle_requirement_text_<?php echo $row["p_id"];?>"></div></td>
<td><a href="javascript:" onclick = "show_delete_yes(this,<?php echo $row['p_id'];?>);false;" >Delete</a>&nbsp&nbsp<a href="editprogram.php?id=<?php echo $row['p_id'];?>">Edit</a></td>
</tr>
<?php
	}
}
?>
</tbody>
</table>
<div>
<?php
if ($totalPage > 1){
	if ($page != 1) {
		echo "<a href='manageprograms.php?major=" . $_GET["major"] . "&entry_year=" . $_GET["entry_year"] . "&starting_course=" . $_GET["starting_course"] . "&page=" . ($page - 1) . "'>Back</a>&nbsp";
	}
	for ( $i = 1; $i <= $totalPage; $i++){
		if ($i == $page){
			echo "&nbsp" . $i . "&nbsp";
		}
		else{
			echo "&nbsp<a href='manageprograms.php?nmajor=" . $_GET["major"] . "&entry_year=" . $_GET["entry_year"] . "&starting_course=" . $_GET["starting_course"] . "&page=" . $i . "'>" . $i . "</a>&nbsp";
		}
	}
	if ($page < $totalPage) {
		echo "&nbsp<a href='manageprograms.php?major=" . $_GET["major"] . "&entry_year=" . $_GET["entry_year"] . "&starting_course=" . $_GET["starting_course"] . "&page=" . ($page + 1) . "'>Next</a>";
	}
}
?>
</div>
</div>
<script>
function toggle_requirement_list(id){
	if ($("#toggle_requirement_link_"+id).text() == "Show"){
		$("#toggle_requirement_link_"+id).text("Hide");
		$.post("findrequirements_byprogram.php",
		{
			p_id:id,
		},
		function(data,status){
			$("#toggle_requirement_text_"+id).html(data);
		});
		$("#toggle_requirement_text_"+id).show();
	}
	else{
		$("#toggle_requirement_link_"+id).text("Show");
		$("#toggle_requirement_text_"+id).hide();
	}
}
function show_delete_yes(link, id){
	var top = $(link).offset().top;
	$("#deleteyes").css("display","block");
	$("#deleteyes").css("top", top);
	$("#p_id").val(id);
}
function hide_delete_yes(){
	$("#deleteyes").css("display","none");
}
</script>
<div id = "deleteyes" style="display:none;">
<div class = "confirm_delete_text">Do you really want to delete it?</div>
<form action = "deleteprogram.php" method = "POST">
<input type = "hidden" name = "id" id = "r_id" />
<input type = "submit" value = "Yes" />
<input type = "button" value = "No" onclick = "hide_delete_yes();false;" />
</form>
</div>
<?php
}
include "footer.php";